﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using log4net;

namespace Dominion.Core.Runners
{
    public class BTree<T>
    {
        private IComparer<T> comparer;
        public BTree(IComparer<T> cpr, Node<T> root)
        {
            comparer = cpr;
            Root = root;

        }
        public Node<T> Root { get; private set; }

        public void Add(Node<T> nodeToAdd)
        {
            Node<T> currentNode = Root;
            int comp = comparer.Compare(currentNode.Value, nodeToAdd.Value);
            if (comp > 0)
            {
                currentNode.Right = nodeToAdd;
                nodeToAdd.Parent = currentNode;
            }
            else
            {
                currentNode.Left = nodeToAdd;
                nodeToAdd.Parent = currentNode;
            }

        }
    }

    public class Node<T>
    {
        public T Value { get; set; }
        public Node<T> Parent { get; set; }
        public Node<T> Left { get; set; }
        public Node<T> Right { get; set; }
    }



    public class JohnStrategy : Strategy
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(JohnStrategy));



        private List<string> orderedBuyCardsChoice = new List<string>
        {
            Kind.Province.ToString(),
            Kind.Gold.ToString(),
            Kind.Duchy.ToString(),
            Kind.Silver.ToString(),
            Kind.Estate.ToString(),
            Kind.Copper.ToString(),
        };


        public override void Run(Player player)
        {
            while (player.BuyCount > 0)
            {
                foreach (string cardName in orderedBuyCardsChoice)
                {
                    if (player.CanBuyCard(cardName))
                    {
                        player.BuyCard(cardName);

                        break;
                    }
                }
            }


            // much more brainstroming
            Thread.Sleep(100);
            if (log.IsDebugEnabled) log.Debug(player);
            player.EndTurn();
        }

        public override void MiliciaReaction(object sender, EventArgs args)
        {
            int cpt = 0;
            IList<Kind> orderedDiscardCardsChoice = new List<Kind>
                    {
                    Kind.Malediction,
                    Kind.Duchy,
                    Kind.Province,
                    Kind.Estate,
                    Kind.Copper,
                    };

            while (Player.Cards.Hand.Count > 3)
            {
                foreach (Kind cardKind in orderedDiscardCardsChoice)
                {
                    var cards = Player.Cards.Hand.Where(c => c.Kind.Equals(cardKind));
                    if (cards.Any())
                    {
                        Card c = cards.First();
                        Player.Cards.Hand.Remove(c);
                        Player.Cards.DiscardCard(c);
                        if (log.IsDebugEnabled) log.Debug(Player.Name + " discard " + c.Name);
                        cpt++;
                        break;
                    }
                }

            }

            Player.Game.Messages.Enqueue(Player.Name + " discard " + cpt + " card(s)");

        }
       
        public override bool IsAuto
        {
            get { return true; }
        }
    }
}
